//
//  ScrollPhotoView+list.m
//  NaRiLSVMT
//
//  Created by zhangmeng on 14-10-27.
//  Copyright (c) 2014年 helloyouth.org. All rights reserved.
#import "ScrollPhotoView+list.h"
#import "ScrollPhotoView+Request.h"
#define MAXLEN 90.0
@implementation ScrollPhotoView (list)
-(void)initDroplist
{
    self.listView = [[DropDownListView alloc] initWithFrame:CGRectMake(80,400,150,0) dataSource:self delegate:self Tag:@"0"];
    self.listView.mSuperView = self;
    self.listView.screenHeight = 2400;
    self.listView.screenWidth = 9600;
    self.listView.flagNumList = 2;
    self.listView.natureNum = 1;
    [self.superview addSubview:self.listView];
}
-(void)flushThumb:(NSInteger)index
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.thumbScreen[self.nowScreen]];
    NSMutableArray *arr = [NSMutableArray arrayWithArray:dic[@"group"]];
    
 //   [self didSectionframe:arr[index][@"groupname"]];
    if([arr count] <= 1){
        NSDictionary *dic = arr[index];
        [arr removeAllObjects];
        arr = [NSMutableArray array];
        [arr addObject:dic];
    }
    else{
        if(!index){
            [arr removeObjectAtIndex:index];
        }
        else{
            NSDictionary *dic = arr[index];
            [arr removeAllObjects];
            arr = [NSMutableArray array];
            [arr addObject:dic];
        }
    }
    
    [self getGroupThumbno:dic[@"screenId"] Grouparr:arr Btnnum:index];
}

#pragma -make DropListDelegate
-(void)chooseAtSection:(NSInteger)section index:(NSInteger)index
{
    self.imageList.image = [UIImage imageNamed:@"scan_uplist.png"];
    [self.listProView removeFromSuperview];
    //关闭交互
    if([self.delegate respondsToSelector:@selector(panTalkinfo)]){
        [self.delegate panTalkinfo];
    }
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.thumbScreen[self.nowScreen]];
    NSMutableArray *arr = [NSMutableArray arrayWithArray:dic[@"group"]];
    
    [self didSectionframe:arr[index][@"groupname"]];
    if([arr count] <= 1){
        NSDictionary *dic = arr[index];
        [arr removeAllObjects];
        arr = [NSMutableArray array];
        [arr addObject:dic];
    }
    else{
    if(!index){
        [arr removeObjectAtIndex:index];
    }
    else{
        NSDictionary *dic = arr[index];
        [arr removeAllObjects];
        arr = [NSMutableArray array];
        [arr addObject:dic];
    }
    }
    self.textFiledView.hidden = YES;
  [self getGroupThumb:dic[@"screenId"] Grouparr:arr Btnnum:index];
}
-(void)didSectionframe:(NSString *)screenname
{
    CGRect s = [screenname boundingRectWithSize:CGSizeMake(150,0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.labList.font} context:nil];
    CGRect r;
    if(s.size.width >= 36){
        r = self.labList.frame;
        r.size.width = s.size.width+2;
        self.labList.frame = r;
        r = self.imageList.frame;
        r.origin.x = self.labList.frame.size.width+15;
        self.imageList.frame = r;
    }
    else{
        r = self.labList.frame;
        r.size.width = 45;
        self.labList.frame = r;
        r = self.imageList.frame;
        r.origin.x = self.labList.frame.size.width+10;
        self.imageList.frame = r;
    }
    if(s.size.width <= MAXLEN){
    r = self.btnList.frame;
    r.size.width = self.imageList.frame.origin.x+self.imageList.frame.size.width+4;
    }
    else
    {
    r = self.labList.frame;
    r.size.width = MAXLEN;
    self.labList.frame = r;
    r = self.imageList.frame;
    r.origin.x = self.labList.frame.size.width+15;
    self.imageList.frame = r;
    r = self.btnList.frame;
    r.size.width = 123;
    }
    self.btnList.frame = r;
    self.labList.text = screenname;
    self.imageList.image = [UIImage imageNamed:@"scan_uplist.png"];
    r = self.allthumb.frame;
    r.origin.x = self.btnList.frame.origin.x+self.btnList.frame.size.width + 10;
    self.allthumb.frame = r;
}
-(NSInteger)numberOfSections
{
    return 1;
}
-(NSInteger)numberOfRowsInSection:(NSInteger)section
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.thumbScreen[self.nowScreen]];
    NSArray *arr = [NSArray arrayWithArray:dic[@"group"]];
    if(arr && [arr count])
    return [dic[@"group"] count];
    else
    return 0;
}
-(NSString *)titleInSection:(NSInteger)section index:(NSInteger) index
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.thumbScreen[self.nowScreen]];
    NSArray *arr = [NSArray arrayWithArray:dic[@"group"]];
    if(arr && [arr count])
        return arr[index][@"groupname"];
    else
        return @"";
}
-(NSInteger)defaultShowSection:(NSInteger)section
{
    return 0;
}
-(void)btnListClick:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    if(self.listView.isOpen){
        [self.listView hideExtendedChooseView];
        self.listView.isOpen = NO;
        [self.listProView removeFromSuperview];
        self.imageList.image = [UIImage imageNamed:@"scan_uplist.png"];
    }
    else{
      //  imagelist.image = [UIImage imageNamed:@"scandownlist.png"];
        [self addSubview:self.listProView];
        self.listView.numButton = btn.tag - SECTION_BTN_TAG_BEGIN;
        [self.superview addSubview:self.listView];
        [self.listView sectionBtnTouch:(UIButton *)sender];
        self.listView.isOpen = YES;
        self.imageList.image = [UIImage imageNamed:@"scandownlist.png"];
    }

}
-(void)tapListView:(UITapGestureRecognizer *)reconiger
{
  
    [self closeListView];
}
//关闭下拉框
-(void)closeListView
{
    if(self.listView.isOpen){
        [self.listView hideExtendedChooseView];
        self.listView.isOpen = NO;
        [self.listProView removeFromSuperview];
        self.imageList.image = [UIImage imageNamed:@"scan_uplist.png"];
     //   imagelist.image = [UIImage imageNamed:@"scan_uplist.png"];
    }
}

@end
